package com.yunshan.mp.service.impl;

import com.yunshan.common.utils.DateUtils;
import com.yunshan.mp.domain.MpIndexBanner;
import com.yunshan.mp.mapper.MpIndexBannerMapper;
import com.yunshan.mp.service.IMpIndexBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 首页banner配置Service业务层处理
 * 
 * @author yunshan
 * @date 2025-01-20
 */
@Service
public class MpIndexBannerServiceImpl implements IMpIndexBannerService 
{
    @Autowired
    private MpIndexBannerMapper mpIndexBannerMapper;

    /**
     * 查询首页banner配置
     * 
     * @param id 首页banner配置主键
     * @return 首页banner配置
     */
    @Override
    public MpIndexBanner selectMpIndexBannerById(Long id)
    {
        return mpIndexBannerMapper.selectMpIndexBannerById(id);
    }

    /**
     * 查询首页banner配置列表
     * 
     * @param mpIndexBanner 首页banner配置
     * @return 首页banner配置
     */
    @Override
    public List<MpIndexBanner> selectMpIndexBannerList(MpIndexBanner mpIndexBanner)
    {
        return mpIndexBannerMapper.selectMpIndexBannerList(mpIndexBanner);
    }

    /**
     * 新增首页banner配置
     * 
     * @param mpIndexBanner 首页banner配置
     * @return 结果
     */
    @Override
    public int insertMpIndexBanner(MpIndexBanner mpIndexBanner)
    {

        //判断上下架时间
        if (mpIndexBanner.getOnTime().before(DateUtils.getNowDate()) && mpIndexBanner.getOffTime().after(DateUtils.getNowDate())){
            mpIndexBanner.setStatus(0);
        }else if (mpIndexBanner.getOffTime().before(DateUtils.getNowDate())){
            mpIndexBanner.setStatus(1);
        }

        mpIndexBanner.setCreateTime(DateUtils.getNowDate());
        return mpIndexBannerMapper.insertMpIndexBanner(mpIndexBanner);
    }

    /**
     * 修改首页banner配置
     * 
     * @param mpIndexBanner 首页banner配置
     * @return 结果
     */
    @Override
    public int updateMpIndexBanner(MpIndexBanner mpIndexBanner)
    {
        //判断上下架时间
        if (mpIndexBanner.getOnTime() != null && mpIndexBanner.getOffTime() != null) {
            if (mpIndexBanner.getOnTime().before(DateUtils.getNowDate()) && mpIndexBanner.getOffTime().after(DateUtils.getNowDate())) {
                mpIndexBanner.setStatus(0);
            } else if (mpIndexBanner.getOffTime().before(DateUtils.getNowDate())) {
                mpIndexBanner.setStatus(1);
            }
        }

        mpIndexBanner.setUpdateTime(DateUtils.getNowDate());
        return mpIndexBannerMapper.updateMpIndexBanner(mpIndexBanner);
    }

    /**
     * 批量删除首页banner配置
     * 
     * @param ids 需要删除的首页banner配置主键
     * @return 结果
     */
    @Override
    public int deleteMpIndexBannerByIds(Long[] ids)
    {
        return mpIndexBannerMapper.deleteMpIndexBannerByIds(ids);
    }

    /**
     * 删除首页banner配置信息
     * 
     * @param id 首页banner配置主键
     * @return 结果
     */
    @Override
    public int deleteMpIndexBannerById(Long id)
    {
        return mpIndexBannerMapper.deleteMpIndexBannerById(id);
    }

    /**
     * 小程序查询首页banner接口
     * @return 结果
     */
    @Override
    public List<String> selectUniList() {
        return mpIndexBannerMapper.selectUniList();
    }

    @Override
    public int updateMpIndexBannerStatus(Integer status) {
        return mpIndexBannerMapper.updateMpIndexBannerStatus(status);
    }


}
